<template>
  <div class="box">
    <div class="header">
      <h2>质控费用汇总报表明细</h2>
      <div style="display: flex; justify-content: flex-end">
        <el-button plain icon="el-icon-upload2" @click="exportData"
          >导出</el-button
        >
      </div>
      <hr />
    </div>
    <div class="content">
      <elltable
        :loading="false"
        :data="tableData.list.data"
        :n="4"
        :th="tableData.fields"
      >
        <template v-if="tableData.fields.length > 0" v-slot:after-columns>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="handleClick(scope.row)"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </template>
      </elltable>
    </div>
    <div class="footer">
      <pagination
        :current-page="form.pageDate.pageNum"
        :page-size="form.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="修改" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="updateParams">
        <el-form-item label="落实客户" label-width="120px">
          <el-select v-model="updateParams.client" style="width: 50%">
            <el-option
              v-for="item in networkname"
              :key="item.id"
              :value="item.clientName"
              :label="item.clientName"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="uodateParams">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  qualitycontrolcostsdetail,
  exportqccDetail,
  undatecbname,
  updateDetailcbname,
} from "@/api/HqFinancePanel";

export default {
  data() {
    return {
      form: {
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      tableData: {
        fields: [],

        list: {
          data: [],
        },
      },
      total: 0,
      loading: null,
      dialogFormVisible: false,
      updateParams: {
        id: null,
        client: "",
      },
      // 结算网点名称
      networkname: [],
    };
  },
  async created() {
    const res = await undatecbname();
    this.networkname = res.data;
  },
  mounted() {
    // if (this.form == null) {
    //   this.$message.error('暂无明细，即将跳转到上一个页面...')
    //   setTimeout(() => {
    //     this.$router.go(-1)
    //     this.loading.close()
    //   }, 2000)
    // } else {
    //   this.getDetail(this.form)
    // }
  },

  beforeDestroy() {
    if (this.loading != null) {
      this.loading.close();
    }
  },
  activated() {
    this.form = this.$store.state.app.zkfytab;
    // // console.log('Component activated')
    // 进行一些操作比如数据重新请求等
    if (this.form == null) {
      this.$message.error("暂无明细，即将跳转到质控费用汇总报表页面...");
      setTimeout(() => {
        this.$router.push({
          path: "QualityControlCosts",
        });
        this.loading.close();
      }, 2000);
    } else {
      this.getDetail(this.form);
      // this.loading.close()
    }
  },
  methods: {
    exportData() {
      // this.$notify.info({
      //   title: '开始准备下载...'
      // })
      exportqccDetail(this.form).then((res) => {
        // const url = window.URL.createObjectURL(new Blob([res]), {
        //   type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        // })
        // const link = document.createElement('a')
        // link.href = url
        // link.setAttribute('download', '质控费用汇总报表明细.xlsx') // 设置下载的文件名
        // document.body.appendChild(link)
        // link.click()
        // link.parentNode.removeChild(link)
        // this.$notify({
        //   title: '下载成功',
        //   type: 'success'
        // })
        this.$notify({
          title: "请前往总部下载中心查看",
          type: "success",
        });
      });
    },
    getDetail(data) {
      this.loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.7)",
      });
      qualitycontrolcostsdetail(data).then((res) => {
        if (res.code === 200) {
          this.tableData = res.data;
          this.total = res.data.list.total;
        } else {
          this.tableData = {
            fields: [],
            list: {
              data: [],
            },
          };
          this.total = 0;
        }
        this.loading.close();
      });
    },
    handleSizeChange(size) {
      this.form.pageDate.pageSize = size;
      this.getDetail(this.form);
    },
    handleCurrentChange(current) {
      this.form.pageDate.pageNum = current;
      this.getDetail(this.form);
    },
    // 修改按钮
    handleClick(row) {
      this.dialogFormVisible = true;
      this.updateParams.id = row.id;
      this.updateParams.client = row.contractArea;
    },
    // 确定修改
    async uodateParams() {
      const res = await updateDetailcbname(this.updateParams);
      if (res.code === 200) {
        this.$message({
          type: "success",
          message: "修改成功",
        });
        this.dialogFormVisible = false;
        this.getDetail(this.form);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  padding: 20px;
}

.header {
  h2 {
    text-align: center;
    color: skyblue;
  }

  hr {
    color: skyblue;
  }
}

.footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
